package com.itcam.chat.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.itcam.chat.domain.ChatProject;
import com.itcam.chat.mapper.ChatProjectMapper;
import com.itcam.chat.service.IChatProjectService;
import com.itcam.chat.utils.FileUtil;
import com.itcam.common.utils.DateUtils;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.RedisVectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itcam.chat.mapper.ChatKnowledgeMapper;
import com.itcam.chat.domain.ChatKnowledge;
import com.itcam.chat.service.IChatKnowledgeService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 知识库管理Service业务层处理
 *
 * @author cammy
 * @date 2024-11-11
 */
@Service
public class ChatKnowledgeServiceImpl implements IChatKnowledgeService {

    @Autowired
    private ChatKnowledgeMapper chatKnowledgeMapper;

    @Autowired
    private ChatProjectMapper chatProjectMapper;

    @Autowired
    private RedisVectorStore ollamaRedisVectorStore;

    @Autowired
    private IChatProjectService chatProjectService;


    /**
     * 查询知识库管理
     *
     * @param knowledgeId 知识库管理主键
     * @return 知识库管理
     */
    @Override
    public ChatKnowledge selectChatKnowledgeByKnowledgeId(Long knowledgeId) {
        return chatKnowledgeMapper.selectChatKnowledgeByKnowledgeId(knowledgeId);
    }

    /**
     * 查询知识库管理列表
     *
     * @param chatKnowledge 知识库管理
     * @return 知识库管理
     */
    @Override
    public List<ChatKnowledge> selectChatKnowledgeList(ChatKnowledge chatKnowledge) {
        return chatKnowledgeMapper.selectChatKnowledgeList(chatKnowledge);
    }

    /**
     * 新增知识库管理
     *
     * @param chatKnowledge 知识库管理
     * @return 结果
     */
    @Override
    public int insertChatKnowledge(ChatKnowledge chatKnowledge) {
        chatKnowledge.setCreateTime(DateUtils.getNowDate());
        return chatKnowledgeMapper.insertChatKnowledge(chatKnowledge);
    }

    /**
     * 修改知识库管理
     *
     * @param chatKnowledge 知识库管理
     * @return 结果
     */
    @Override
    public int updateChatKnowledge(ChatKnowledge chatKnowledge) {
        chatKnowledge.setUpdateTime(DateUtils.getNowDate());
        return chatKnowledgeMapper.updateChatKnowledge(chatKnowledge);
    }

    /**
     * 批量删除知识库管理
     *
     * @param knowledgeIds 需要删除的知识库管理主键
     * @return 结果
     */
    @Override
    public int deleteChatKnowledgeByKnowledgeIds(Long[] knowledgeIds) {
        return chatKnowledgeMapper.deleteChatKnowledgeByKnowledgeIds(knowledgeIds);
    }

    /**
     * 删除知识库管理信息
     *
     * @param knowledgeId 知识库管理主键
     * @return 结果
     */
    @Override
    public int deleteChatKnowledgeByKnowledgeId(Long knowledgeId) {
        return chatKnowledgeMapper.deleteChatKnowledgeByKnowledgeId(knowledgeId);
    }

    /**
     * 知识库文件上传
     *
     *  1.需要将上传的文件内容保存到本地数据库    —— 优化：MinIO OSS
     *
     *  2.需要将知识库的额内容上传到向量数据库
     * @param chatKnowledge
     * @param file
     * @return
     */
    @Override
    public Long upload(ChatKnowledge chatKnowledge, MultipartFile file) {
        // 获取与当前知识库绑定的项目信息
        Long projectId = chatKnowledge.getProjectId();
        ChatProject chatProject = chatProjectMapper.selectChatProjectByProjectId(projectId);

        // 获取文件名 及 内容
        String filename = file.getOriginalFilename();
        String content = FileUtil.getContentFromText(file);

        // 构建chatKnowledge值
        chatKnowledge.setProjectId(chatProject.getProjectId());
        chatKnowledge.setFileName(filename);
        chatKnowledge.setContent(content);
        chatKnowledge.setCreateTime(new Date());
        // 保存到数据库
        chatKnowledgeMapper.insertChatKnowledge(chatKnowledge);

        //this.knowledgeService.insertChatKnowledge(chatKnowledge);
        Long knowledgeId = chatKnowledge.getKnowledgeId();

        // 上传到redis向量数据库
        Document document = new Document(knowledgeId.toString(), content, Map.of("projectId", chatProject.getProjectId()));
        ollamaRedisVectorStore.add(List.of(document));

        return knowledgeId;
    }

    /**
     * 知识库文件删除
     *
     * @param projectId
     * @param knowledgeId
     * @return
     */
    @Override
    public Boolean remove(Long projectId, Long knowledgeId) {
        // 根据项目id查询项目，获取类型及具体模型
        ChatProject chatProject = this.chatProjectService.selectChatProjectByProjectId(projectId);
        // 数据库删除知识库
        chatKnowledgeMapper.deleteChatKnowledgeByKnowledgeId(knowledgeId);
        // 删除redis向量数据库中对应的文档
        Boolean b = ollamaRedisVectorStore.delete(List.of(knowledgeId.toString())).get();
        return b;
    }
}
